#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <list>
#include <time.h>
#include "charsetconvert.h"
//#include "senwordpro.h"
#include "senwordtrie.h"
using namespace std;

#define CSTTIMES 100000

void usage()
{
	printf( "./senmain  senworddictfile   contentfile\n" );
}

int main(int argc, char ** argv)
{
	if( argc < 3 )
	{
		usage();
		return -1;
	}

	CSenWordPro csp;
	CCharSetConvert cs;
	struct stat st;
	char * szRetSenWord = NULL;
	int fd;
	char * o_buf = NULL;
	char * s = NULL;
	int iRes = 0;
	int iCircleCounts = CSTTIMES;
	time_t curTime = 0;

	csp.init( argv[1] );

	if((fd = open(argv[2], O_RDONLY, 0644)) > 0)
	{
		if(fstat(fd, &st) == 0 && st.st_size > 0 && (s = (char *)calloc(1, st.st_size + 1)))
		{
			unsigned int o_size = st.st_size*3 + 1;
			o_buf = (char*)malloc(o_size);
			
			if(read(fd, s, st.st_size) > 0
				&& (iRes=cs.CharsetConvert(s, st.st_size, o_buf, o_size /*, "UTF-8"*/ )) > 0)
			{
				curTime =  time(NULL);
				while( iCircleCounts-->0 )	
				{
					if( 0==csp.querySenWord( (const char*)o_buf, &szRetSenWord ) )
					{
						printf( "query senword:[%s]\n", szRetSenWord );
					}
					else
					{
						//printf( "not found.\n" );
					}
				}
				printf( "compute %d times,consume time:%u.\n", CSTTIMES, time(NULL)-curTime );
			}
			free(s);
			s = NULL;
			free(o_buf);
			o_buf = NULL;
		}
		close(fd);
	}
	else
	{
		printf( "open content fail.\n" );
		return -1;
	}

/*
      SenWordTrie t;
      t.insert("aa"); 
      t.insert("abacus");
      t.insert("abalone"); 
      t.insert("abandon");
      t.insert("abandoned"); 
      t.insert("abashed");
      t.insert("abate");
      t.insert("VERB"); 
      t.insert("this");
      t.insert("PRON");

      char * entry = NULL;

	printf( "----------------------start\n" );
      t.dump();
	printf( "----------------------end\n" );

    if (0==t.querySenWord("this", &entry))
        cout<<"'this' was found. pos: "<<entry<<endl;
    if (0==t.querySenWord("abate", &entry))
        cout<<"'abate' is found. pos: "<<entry<<endl;
    if (0==t.querySenWord("baby", &entry))
        cout<<"'baby' is found. pos: "<<entry<<endl;
    else
        cout<<"'baby' does not exist at all!"<<endl;
    
    if (0==t.querySenWord("aa", &entry))
        cout<<"'aa was found. pos: "<<entry<<endl;

    if (0==t.querySenWord("aven", &entry))
        cout<<"'aven' is found. pos: "<<entry<<endl;
    else
        cout<<"'aven' does not exist at all!"<<endl;

    if (0==t.querySenWord("VERBfjlsaoe", &entry))
        cout<<"'versbksjflk was found. pos: "<<entry<<endl;

    //char szTestReplace[] = "abate";
    char szTestReplace[] = "aaa VERBaven";
    if( t.replaceSenWord2(szTestReplace)>0 )
    {
        printf( "replace end:%s\n", szTestReplace );
    }
    else
    {
        printf( "replace not change\n" );
    }
*/

	return 0;
}
 
